package cn.lj.org.controller;

import cn.lj.base.util.AjaxResult;
import cn.lj.base.util.PageList;
import cn.lj.org.domain.Employee;
import cn.lj.org.query.EmployeeQuery;
import cn.lj.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //@Controller+@ResponseBody(转换json)
@RequestMapping("/emp")
@Api(tags = "员工接口",description = "员工接口详细描述")
public class EmployeeController {
    /*注入service对象*/
    @Autowired
    private IEmployeeService employeeService;
    /*根据ID查询数据*/
    @GetMapping("/{id}")
    @ApiOperation(value = "员工查找",notes = "根据ID查找")
    public Employee findById(@PathVariable("id") Long id){
        Employee employee = employeeService.getById(id);
        return employee;
    }
    @GetMapping
    @ApiOperation(value = "员工查找",notes = "查找所有员工")
    public List<Employee> findAll(){
        List<Employee> list = employeeService.getAll();
        return list;
    }
    /*高级查询，post请求，@RequestBody：请求的json数据转化为对象数据*/
    @PostMapping
    @ApiOperation(value = "员工高级查询",notes = "员工高级查询")
    public PageList<Employee> findQuery(@RequestBody EmployeeQuery query){
        PageList<Employee> data = employeeService.queryData(query);
        return data;
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除员工",notes = "根据ID删除员工")
    public AjaxResult del(@PathVariable("id")Long id){
        try {
            employeeService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setAjaxResult(e.getMessage());
        }
    }
    @PatchMapping
    @ApiOperation(value = "删除员工",notes = "根据ID删除员工")
    public AjaxResult patchdel(@RequestBody List<Long> ids){
        try {
            employeeService.patchdel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setAjaxResult(e.getMessage());
        }
    }
    @PutMapping//添加或者修改
    @ApiOperation(value = "员工的添加或者修改",notes = "员工的添加或者修改")
    public AjaxResult updateOrSave(@RequestBody Employee employee){
        try {
            if(employee.getId() == null){
                employeeService.add(employee);
            }else {
                employeeService.upadte(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setAjaxResult(e.getMessage());
        }
    }
}
